# 主程序
from PyQt5 import QtCore
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.uic import loadUiType
import mydbutil

# ui logic分离
ui, _ = loadUiType('main.ui')  # 使用下划线接受另外一个参数但不使用


class MainApp(QMainWindow, ui):
    # 定义构造方法
    def __init__(self):
        # 显式调用父类构造方法
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.handle_ui_change()
        self.handle_buttons()
        self.open_book_tab()
        self.show_category()
        self.show_author()
        self.show_press()
        self.show_category_combobox()
        self.show_author_combobox()

    # 处理button消息与槽的通讯
    def handle_buttons(self):
        self.themeButton.clicked.connect(self.show_themes)
        self.theme_change_Button.clicked.connect(self.hide_themes)
        self.bookButton.clicked.connect(self.open_book_tab)
        self.settingButton.clicked.connect(self.open_setting_tab)
        self.userButton.clicked.connect(self.open_user_tab)
        self.dayButton.clicked.connect(self.open_day_tab)
        self.add_category_Button.clicked.connect(self.add_category)
        self.add_author_Button.clicked.connect(self.add_author)
        self.add_press_Button.clicked.connect(self.add_press)
        # 添加图书
        self.add_book_save_Button.clicked.connect(self.add_book)

    # 选项卡联动
    def open_book_tab(self):
        self.tabWidget.setCurrentIndex(0)

    def open_setting_tab(self):
        self.tabWidget.setCurrentIndex(1)

    def open_user_tab(self):
        self.tabWidget.setCurrentIndex(2)

    def open_day_tab(self):
        self.tabWidget.setCurrentIndex(3)

    # 隐藏主题
    def hide_themes(self):
        self.theme_groupBox.hide()

    # 显示主题
    def show_themes(self):
        # print('显示主题框')
        self.theme_groupBox.show()

    def handle_ui_change(self):
        # 主题隐藏
        self.hide_themes()
        # 第一层选项卡不显示
        self.tabWidget.tabBar().setVisible(False)

    # 1.添加类别、作者、出版社
    def add_category(self):
        # 数据库操作流程
        # 1.获取数据库连接
        conn = mydbutil.get_conn()
        # 2.获取cursor
        cur = conn.cursor()
        # 3.sql
        category_name = self.add_category_name.text()
        # 注意sql语句的写法，不然会报错
        sql = "insert into category(category_name) values ('{0}')".format(category_name)
        # print(sql)
        # 4.执行语句
        cur.execute(sql)
        # 5.提交
        conn.commit()
        # 6.关闭资源
        conn.close()
        # 7.消息提示
        self.statusBar().showMessage('类别添加成功')
        self.add_category_name.setText('')
        self.show_category()

        pass

    def add_author(self):
        # 数据库操作流程
        # 1.获取数据库连接
        conn = mydbutil.get_conn()
        # 2.获取cursor
        cur = conn.cursor()
        # 3.sql
        author_name = self.add_author_name.text()
        sql = "insert into author(author_name) values ('{0}')".format(author_name)
        # 4.执行语句
        cur.execute(sql)
        # 5.提交
        conn.commit()
        # 6.关闭资源
        conn.close()
        # 7.消息提示
        self.statusBar().showMessage('作者添加成功')
        self.add_author_name.setText('')
        self.show_author()

        pass

    def add_press(self):
        # 数据库操作流程
        # 1.获取数据库连接
        conn = mydbutil.get_conn()
        # 2.获取cursor
        cur = conn.cursor()
        # 3.sql
        press_name = self.add_press_name.text()
        # 注意sql语句的写法，不然会报错
        sql = "insert into press(press_name) values ('{0}')".format(press_name)
        # print(sql)
        # 4.执行语句
        cur.execute(sql)
        # 5.提交
        conn.commit()
        # 6.关闭资源
        conn.close()
        # 7.消息提示
        self.statusBar().showMessage('出版社添加成功')
        self.add_press_name.setText('')
        self.show_press()

        pass

    def show_category(self):
        # 数据库操作流程
        # 1.获取连接
        conn = mydbutil.get_conn()
        # 2.获取cursor
        cur = conn.cursor()
        # 3.写sql语句
        sql = "SELECT category_name FROM category"
        # 4.执行语句
        result = cur.execute(sql)
        data = result.fetchall()
        conn.close()

        # 将查询的数据添加到程序中
        if data:
            self.category_table.setRowCount(0)
            self.category_table.insertRow(0)
            for row, form in enumerate(data):
                for column, item in enumerate(form):
                    self.category_table.setItem(row, column, QTableWidgetItem(str(item)))
                    column += 1

                row_position = self.category_table.rowCount()
                self.category_table.insertRow(row_position)

    def show_author(self):
        # 数据库操作流程
        # 1.获取连接
        conn = mydbutil.get_conn()
        # 2.获取cursor
        cur = conn.cursor()
        # 3.写sql语句
        sql = "SELECT author_name FROM author"
        # 4.执行语句
        result = cur.execute(sql)
        data = result.fetchall()
        conn.close()

        # 将查询的数据添加到程序中
        if data:
            self.author_table.setRowCount(0)
            self.author_table.insertRow(0)
            for row, form in enumerate(data):
                for column, item in enumerate(form):
                    self.author_table.setItem(row, column, QTableWidgetItem(str(item)))
                    column += 1

                row_position = self.author_table.rowCount()
                self.author_table.insertRow(row_position)

    def show_press(self):
        # 数据库操作流程
        # 1.获取连接
        conn = mydbutil.get_conn()
        # 2.获取cursor
        cur = conn.cursor()
        # 3.写sql语句
        sql = "SELECT press_name FROM press"
        # 4.执行语句
        result = cur.execute(sql)
        data = result.fetchall()
        conn.close()

        # 将查询的数据添加到程序中
        if data:
            self.press_table.setRowCount(0)
            self.press_table.insertRow(0)
            for row, form in enumerate(data):
                for column, item in enumerate(form):
                    self.press_table.setItem(row, column, QTableWidgetItem(str(item)))
                    column += 1

                row_position = self.press_table.rowCount()
                self.press_table.insertRow(row_position)

    # 2.将基础数据和combobox绑定
    def show_category_combobox(self):
        # 数据库操作流程
        # 1.获取连接
        conn = mydbutil.get_conn()
        # 2.获取cursor
        cur = conn.cursor()
        # 3.写sql语句
        sql = "SELECT category_name FROM category"
        # 4.执行语句
        result = cur.execute(sql)
        data = result.fetchall()
        conn.close()

        # 将查询的数据添加到选择框中
        if data:
            # 清空
            self.add_book_category.clear()
            self.edit_book_category.clear()
            for category in data:
                self.add_book_category.addItem(category[0])
                self.edit_book_category.addItem(category[0])

    def show_author_combobox(self):
        # 数据库操作流程
        # 1.获取连接
        conn = mydbutil.get_conn()
        # 2.获取cursor
        cur = conn.cursor()
        # 3.写sql语句
        sql = "SELECT author_name FROM author"
        # 4.执行语句
        result = cur.execute(sql)
        data = result.fetchall()
        conn.close()

        # 将查询的数据添加到选择框中
        if data:
            # 清空
            self.add_book_author.clear()
            self.edit_book_author.clear()
            for category in data:
                self.add_book_author.addItem(category[0])
                self.edit_book_author.addItem(category[0])

    def add_book(self):

        pass


def main():
    # 加上下面这句话让qtdesinger设计效果与实际效果一致，自适应
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
    app = QApplication([])
    window = MainApp()
    window.show()
    app.exec_()


if __name__ == '__main__':
    main()
